package com.member.web.controller.shop;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.annotation.Excel;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.enums.VerificationStatusEnum;
import com.member.common.utils.DateUtils;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizActivity;
import com.member.shop.domain.SysBizActivityEnroll;
import com.member.shop.service.ISysBizActivityEnrollService;
import com.member.shop.vo.FieldData;
import net.bytebuddy.ByteBuddy;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.List;

/**
 * 【活动报名】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-01-09
 **/
@RestController
@RequestMapping("/shop/enrollActivity")
public class SysBizActivityEnrollController extends BaseController {
    @Resource
    private ISysBizActivityEnrollService sysBizActivityEnrollService;

    private void setTenantId(SysBizActivityEnroll sysBizActivityEnroll) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizActivityEnroll.setTenantId(tenantId);
        }
    }

    /**
     * 查询活动报名列表
     */
    @PreAuthorize("@ss.hasPermi('shop:enrollActivity:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizActivityEnroll sysBizActivityEnroll) {
        startPage();
        setTenantId(sysBizActivityEnroll);
        List<SysBizActivityEnroll> list = sysBizActivityEnrollService.selectSysBizActivityEnrollList(sysBizActivityEnroll);
        return getDataTable(list);
    }

    /**
     * 导出活动报名列表
     */
    @PreAuthorize("@ss.hasPermi('shop:enrollActivity:export')")
    @Log(title = "活动报名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizActivityEnroll sysBizActivityEnroll) throws Exception {
        setTenantId(sysBizActivityEnroll);
        List<SysBizActivityEnroll> list = sysBizActivityEnrollService.selectSysBizActivityEnrollList(sysBizActivityEnroll);
        if (!list.isEmpty()) {
            for (SysBizActivityEnroll bizActivityEnroll : list) {
//                bizActivityEnroll.setEnrollUserTypeName(bizActivityEnroll.getEnrollUserType()==0?"新用户":"老用户");
//                bizActivityEnroll.setEnrollStatusName(bizActivityEnroll.getEnrollStatus()==0?"已报名":"取消报名");
                bizActivityEnroll.setVerificationStatusName(bizActivityEnroll.getVerificationStatus() == 0 ? "待核销" : "已核销");
                List<FieldData> fieldDataList = JSON.parseArray(bizActivityEnroll.getInfo(), FieldData.class);
                if (CollectionUtils.isNotEmpty(fieldDataList)) {
                    for (FieldData fieldData : fieldDataList) {
                        if (fieldData.getName().equals("姓名")) {
                            bizActivityEnroll.setXm(fieldData.getValue());
                        }
                        if (fieldData.getName().equals("身份证号")) {
                            bizActivityEnroll.setSfzh(fieldData.getValue());
                        }
                        if (fieldData.getName().equals("电话")) {
                            bizActivityEnroll.setDh(fieldData.getValue());
                        }
                        if (fieldData.getName().equals("新人还是加盟商")) {
                            bizActivityEnroll.setXrhsjms(fieldData.getValue());
                        }
                        if (fieldData.getName().equals("推荐人姓名")) {
                            bizActivityEnroll.setTjrxm(fieldData.getValue());
                        }
                        if (fieldData.getName().equals("地址")) {
                            bizActivityEnroll.setDz(fieldData.getValue());
                        }
                        if (fieldData.getName().equals("第几组")) {
                            bizActivityEnroll.setDjz(fieldData.getValue());
                        }
                    }
                }
            }
        }
        ExcelUtil<SysBizActivityEnroll> util = new ExcelUtil<SysBizActivityEnroll>(SysBizActivityEnroll.class);
        util.exportExcel(response, list, "活动报名数据");
    }

    /**
     * 获取活动报名详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizActivityEnrollService.selectSysBizActivityEnrollById(id));
    }

    /**
     * 获取活动报名总金额
     */
    @GetMapping(value = "/paymentAmount")
    public AjaxResult paymentAmount(SysBizActivityEnroll sysBizActivityEnroll) {
        BigDecimal bigDecimal = sysBizActivityEnrollService.paymentAmount(sysBizActivityEnroll);
        return success(bigDecimal);
    }


    /**
     * 新增活动报名
     */
    @PreAuthorize("@ss.hasPermi('shop:enrollActivity:add')")
    @Log(title = "活动报名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizActivityEnroll sysBizActivityEnroll) {
        return toAjax(sysBizActivityEnrollService.insertSysBizActivityEnroll(sysBizActivityEnroll));
    }

    /**
     * 修改活动报名
     */
    @PreAuthorize("@ss.hasPermi('shop:enrollActivity:edit')")
    @Log(title = "活动报名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizActivityEnroll sysBizActivityEnroll) {
        return toAjax(sysBizActivityEnrollService.updateSysBizActivityEnroll(sysBizActivityEnroll));
    }

    /**
     * 活动报名核销
     */
    @PreAuthorize("@ss.hasPermi('shop:enrollActivity:edit')")
    @Log(title = "活动报名核销", businessType = BusinessType.UPDATE)
    @PostMapping("/writeOffCode")
    public AjaxResult writeOffCode(@RequestBody SysBizActivityEnroll sysBizActivityEnroll) {
        sysBizActivityEnroll.setWriteOffTime(DateUtils.getNowDate());
        // 判断和code是否一致
        SysBizActivityEnroll sysBizActivityEnroll1 = sysBizActivityEnrollService.selectSysBizActivityEnrollById(sysBizActivityEnroll.getId());
        if (!sysBizActivityEnroll.getWriteOffCode().equals(sysBizActivityEnroll1.getWriteOffCode())) {
            return error("核销码错误");
        }
        sysBizActivityEnroll.setVerificationStatus(VerificationStatusEnum.VERIFIED.getKey());
        return toAjax(sysBizActivityEnrollService.updateSysBizActivityEnroll(sysBizActivityEnroll));
    }

    /**
     * 删除活动报名
     */
    @PreAuthorize("@ss.hasPermi('shop:enrollActivity:remove')")
    @Log(title = "活动报名", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysBizActivityEnrollService.deleteSysBizActivityEnrollByIds(ids));
    }
}
